<template>
  <div class="">
    <el-card>
      <div style="display: flex; justify-content: space-between">
        <el-button type="primary" @click="addGgm">添加</el-button>
        <div>
          <el-input
            v-model="keyword"
            style="width: 240px"
            placeholder="请输入剧本"
            clearable
            @clear="clear"
            @change="enter"
          />
          <el-button type="primary" @click="search" style="margin-left: 10px"
            >搜索</el-button
          >
        </div>
      </div>

      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="index" label="序号" width="180" />
        <el-table-column prop="script_name" label="剧本名" width="180" />
        <el-table-column label="封面图" width="180" v-slot="{ row }">
          <!-- :preview-src-list="['http://10.200.26.30:4000/' + row.cover]" -->
          <el-image
            style="width: 70px; height: 100px"
            :src="URL + '/' + row.cover"
          />
        </el-table-column>
        <el-table-column prop="price" label="价格每人(元)" width="180" />
        <el-table-column
          prop="address"
          label="分类"
          width="180"
          v-slot="{ row }"
        >
          <el-tag
            v-for="item in row.itemCateNameArr"
            :key="item"
            style="margin: 0 2px"
            >{{ item }}</el-tag
          >

          {{ row.cate_ids }}
        </el-table-column>
        <el-table-column prop="address" label="操作" v-slot="{ row }">
          <el-button type="info" @click="opendetail(row.id)">查看</el-button>
          <el-button type="primary" @click="edit(row.id)">编辑</el-button>
          <el-button type="danger" @click="del(row.id)">删除</el-button>
        </el-table-column>
      </el-table>
      <el-pagination
        small
        background
        layout="prev, pager, next"
        :total="total"
        class="mt-4"
        :page-size="pageSize"
        @change="changePage"
        style="display: flex; justify-content: center"
      />
    </el-card>
    <el-dialog
      v-model="centerDialogVisible"
      title="Card"
      width="600"
      align-center
    >
      <el-card style="width: 500px">
        <template #header>
          <div class="card-header">
            <span>剧本名：{{ cardInfo.script_name }}</span>
          </div>
        </template>
        <el-form
          ref="ruleFormRef"
          style="max-width: 600px"
          status-icon
          label-width="auto"
          class="demo-ruleForm"
        >
          <el-form-item label="角色">
            <div style="display: flex; width: 100%" overflow-x="auto">
              <el-card
                style="max-width: 100px; margin: 0 10px"
                v-for="card in roleList"
                :key="card.id"
              >
                <template #header>{{ card.role_name }} </template>
                <img :src="URL + '/' + card.img" style="width: 100%" />
              </el-card>
            </div>
          </el-form-item>
          <el-form-item label="介绍">
            <div>{{ cardInfo.content }}</div>
          </el-form-item>
        </el-form>

        <template #footer
          ><div
            style="
              display: flex;
              align-items: center;
              justify-content: space-between;
              width: 50%;
            "
          >
            <div>
              <svg-icon icon-class="hot" size="30" />热度：{{ cardInfo.heat }}
            </div>
            <div>
              <svg-icon icon-class="aixin" size="25" />想玩的人数：{{
                cardInfo.heat
              }}
            </div>
          </div></template
        >
      </el-card>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="centerDialogVisible = false">Cancel</el-button>
          <el-button type="primary" @click="centerDialogVisible = false">
            Confirm
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
const URL = import.meta.env.VITE_APP_API_URL;
import { reqGgmList, reqGgmDel } from "@/api/ggm/index";
import { ReqGetRole } from "@/api/ggmroles/model";
import { reqDelRole, reqGetRole } from "@/api/ggmroles/index";
import { ResGgmList, CarInfo } from "@/api/ggm/model";
import { useRoute, useRouter } from "vue-router";
const $router = useRouter();
const centerDialogVisible = ref<boolean>(false);
const page = ref<number>(1);
const pageSize = ref<number>(4);
const total = ref<number>(0);
const tableData = ref<ResGgmList[]>([]);
const cardInfo = ref<CarInfo>({
  id: 0,
  script_name: "",
  price: "",
  content: "",
  cover: "",
  difficulty_id: 0,
  cate_name: "",
});
const roleList = ref<ReqGetRole[]>([]);
const keyword = ref<string>("");

//清除搜索内容
const clear = () => {
  getGgmList();
};

// 搜索
const search = () => {
  getGgmList(keyword.value);
};
const enter = () => {
  getGgmList(keyword.value);
};

// 添加剧本
const addGgm = () => {
  delRole();
  $router.push("/playmanager/playadd");
};

const opendetail = async (id: number) => {
  console.log(id);
  let result = await reqGgmList({
    page: 1,
    pageSize: pageSize.value,
    id,
  });
  cardInfo.value = result.rows[0];
  let data = await reqGetRole(id);
  roleList.value = data.rows;
  centerDialogVisible.value = true;
};

const edit = (id: number) => {
  delRole();
  $router.push("/playmanager/playedit?id=" + id);
};

const del = async (mmg_id: number) => {
  ElMessageBox.confirm("确定要删除该剧本吗？", "Warning", {
    type: "warning",
  })
    .then(async () => {
      await reqGgmDel(mmg_id);
      ElMessage({
        message: "删除成功",
        type: "success",
      });
      getGgmList();
    })
    .catch(() => {});
};

// 分页
const changePage = (pageNum: number) => {
  page.value = pageNum;
  getGgmList();
};

// 获取剧本列表
const getGgmList = async (keyword = "") => {
  let result = await reqGgmList({
    page: page.value,
    pageSize: pageSize.value,
    keyword,
  });
  total.value = result.count;
  result.rows.forEach((item) => {
    if (item.cate_name) {
      item.itemCateNameArr = item.cate_name.split(",");
    }
  });
  tableData.value = result.rows;
};
// 删除角色
const delRole = async () => {
  await reqDelRole({ mmg_id: 0 });
};

onMounted(async () => {
  getGgmList();
});
</script>

<style scoped lang="scss"></style>
